const app = getApp()
Page({
  data: {
    accountInfo:{},
    priceList: [
      {
        price: '1,000',
        money:1000,
        checked: true
      },
      {
        price: '2,000',
        money:2000,
        checked: false
      },
      {
        price: '5,000',
        money:5000,
        checked: false
      },
      {
        price: '10,000',
        money:10000,
        checked: false
      },
      {
        price: '20,000',
        money:20000,
        checked: false
      },
      {
        price: '50,000',
        money:50000,
        checked: false
      },
      {
        price: '100,000',
        money:100000,
        checked: false
      }
    ],
    paySuccess:false
  },
  onLoad: function (options) {
    this.getAccountInfo()
  },
  bindPrice(e) {
    let i = e.currentTarget.dataset.index
    this.data.priceList.forEach((item, j) => {
      if (i == j) {
        console.log(i)
        this.setData({
          ['priceList[' + i + '].checked']: true
        })
      } else {
        console.log(j)
        this.setData({
          ['priceList[' + j + '].checked']: false
        })
      }
    })
  },
  bindagreement() {
    wx.navigateTo({
      url: '../agreement/agreement'
    })
  },
  recharge() {
    let that = this
    let money = 1000
    this.data.priceList.forEach(item=>{
      if(item.checked){
        money = item.money
      }
    })
    let data = {
      money:money*100
    }
    that.setData({
      paySuccess:false
    })
    app.request('ugc/pay/miniAppPayOfRecharge', data).then(res => {
      console.log(res)
      wx.hideLoading()
      wx.requestPayment({
        'timeStamp': res.content.timeStamp,
        'nonceStr': res.content.nonceStr,
        'package': 'prepay_id=' + res.content.prepayId,
        'signType': res.content.signType,
        'paySign': res.content.paySign,
        success(data) {
          console.log(data)
          if (data.errMsg == 'requestPayment:ok') {
            wx.showLoading({
              title:'正在充值中...'
            })
            let confirmPay = setInterval(() => {
              that.getPayState(res.content.outputOrderId)
              if(that.data.paySuccess){
                console.log(333)
                clearInterval(confirmPay)
                // that.getAccountInfo()
                wx.switchTab({
                  url:'../member/member'
                })
                // wx.hideLoading()
              }
            }, 2000);
          }else{
            console.log('取消支付')
          }
        }
      })
    })
  },
  getPayState(id){
    let that = this
    app.request('ugc/pay/checkPayResultOfRecharge?outputOrderId='+id).then(res=>{
      wx.hideLoading()
      if(res.success){
        if(res.content){
          that.setData({
            paySuccess:true
          })
          
        }else{
          console.log('未收到支付结果')
        }
      }
    })
  },
  getAccountInfo() {
    app.request('ugc/getAccountInfo').then(res => {
      console.log(res)
      wx.hideLoading()
      if (res.success) {
        this.setData({
          accountInfo: res.content
        })
      } else {
        console.log(res.message)
      }
    })
  },
  back(){
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    console.log(prevPage)
    if(prevPage.route=='pages/member/member'){
      prevPage.getAccountInfo()
    }
    wx.navigateBack()
  }
})